package com.songxh.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.songxh.pojo.Clazz;
import com.songxh.service.ClazzService;
import com.songxh.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author songxh
 * @create 2022-04-18-19:36
 */
@Api("Clazz控制器")
@RestController
@RequestMapping("/sms/clazzController")
public class ClazzController {

    @Autowired
    private ClazzService clazzService;


    @ApiOperation("获取班级信息")
    @GetMapping("/getClazzsByOpr/{pageNo}/{pageSize}")
    public Result getClazzsByOpr(
            @ApiParam("页码")
            @PathVariable("pageNo")Integer pageNo,
            @ApiParam("每一页的页数")
            @PathVariable("pageSize")Integer pageSize,
            String gradeName,
            String name
    ){
        Page<Clazz> page=new Page<>(pageNo,pageSize);
        IPage<Clazz> pageRes =clazzService.getClazzsByOpr(page,name,gradeName);
        return Result.ok(pageRes);
    }

    @ApiOperation("添加或修改班级信息")
    @PostMapping("/saveOrUpdateClazz")
    private Result saveOrUpdateClazz(@ApiParam("该Clazz信息的JSON内容")@RequestBody Clazz clazz){
        //实现服务层的增加与修改操作
        //该方法在IService中已经存在，故而直接用即可
        clazzService.saveOrUpdate(clazz);
        return Result.ok();
    }


    @ApiOperation("删除或批量删除班级信息")
    @DeleteMapping("/deleteClazz")
    public Result deleteClazz(@ApiParam("需要删除的Clazz的id值的JSON集合")@RequestBody List<Integer> ids){
        // .removeByIds(ids)该方法来自IService，由mybatisplus提供
        clazzService.removeByIds(ids);
        return Result.ok();
    }

    @ApiOperation("获取班级信息的集合")
    @GetMapping("/getClazzs")
    public Result getClazzs(){
        List<Clazz> list = clazzService.list();
        return Result.ok(list);
    }

}
